package com.example.basefream.controller;


/*
* excel写入工具类
* */

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.basefream.entity.BizAlertUrl;
import com.example.basefream.entity.CmaPcArea;
import com.example.basefream.service.BizAlertUrlService;
import com.example.basefream.service.CmaPcAreaService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Api(tags = {"使用EasyExcel实现数据导出多sheet"})
@RestController
@RequestMapping("/excel")
public class ExcelWrController {

    @Autowired
    private CmaPcAreaService cmaPcAreaService;
    @Autowired
    private BizAlertUrlService bizAlertUrlService;
    @RequestMapping("/Wexcel")
    public String PathExcel(HttpServletResponse response){

        String fileName="/Users/l-mac/Desktop/User.xlsx";
        ExcelWriter excelWriter=null;
        excelWriter = EasyExcel.write(fileName).build();
        QueryWrapper<CmaPcArea> cmaPcAreaQueryWrapper=new QueryWrapper<>();
        cmaPcAreaQueryWrapper.last("limit 0,100");
        List<CmaPcArea> list=cmaPcAreaService.list(cmaPcAreaQueryWrapper);
        WriteSheet writeSheet = EasyExcel.writerSheet(0, "区域").head(CmaPcArea.class).build();
        excelWriter.write(list,writeSheet);
        QueryWrapper<BizAlertUrl> bizAlertUrlQueryWrapper=new QueryWrapper<>();
        List<BizAlertUrl> bizAlertUrls=bizAlertUrlService.list(bizAlertUrlQueryWrapper);
        WriteSheet writeSheets = EasyExcel.writerSheet(1, "告警").head(BizAlertUrl.class).build();
        excelWriter.write(bizAlertUrls,writeSheets);

        excelWriter.finish();


        return "";
    }
}
